package com.example.a22310.myapplication.Activity;

//红色的R解决不了
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.example.a22310.myapplication.R;
import com.example.a22310.myapplication.ViewModel.Activity.ActivityApplyViewModel;
import com.example.a22310.myapplication.databinding.ActivityApplyBinding;

public class ActivityApply extends AppCompatActivity {
    private ActivityApplyBinding binding;

    private String FILENAME = "yuejian'data";
    private int userid;
    Bundle bundle;
    int number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_apply);
        Intent intent = getIntent();
        bundle = intent.getExtras();
        int id = bundle.getInt("activityid");
        initialize();
        new ActivityApplyViewModel(this,binding,userid,id,number);


    }

    private void initialize(){
        binding.name.setText(bundle.getString("name"));
        int i  = bundle.getInt("type");
        switch (i){
            case 0:
                binding.type.setText("焚书坑儒");
                break;
            case 1:
                binding.type.setText("菜鸟");
                break;
            case 2:
                binding.type.setText("入门");
                break;
            case 3:
                binding.type.setText("晋级");
                break;
            default:
                break;
        }
        binding.price.setText(bundle.getString("price"));
        binding.must.setText(bundle.getString("must"));
        //    int a = Integer.parseInt(binding.number.getText().toString());
        userid = getUserid();
        number = Integer.parseInt(binding.number.getText().toString());
        binding.subtraction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (number==0);
                else  number--;
                binding.number.setText(number+"");
                binding.sum.setText(number*Integer.parseInt(bundle.getString("price"))+"");
            }
        });

        binding.addtion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                number++;
                binding.number.setText(number+"");
                binding.sum.setText(number*Integer.parseInt(bundle.getString("price"))+"");
            }
        });

    }
    private int  getUserid(){
        int newsNumber;
        SharedPreferences sharedPreferences = getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
        newsNumber = sharedPreferences.getInt("userId",0);
        return newsNumber;
    }

}